package com.zx.mobile.service.proxy;

import com.zx.map.service.MessageSendLogService;
import com.zx.map.service.model.MerchantInfoModel;
import com.zx.mobile.MobileCommonHelper;
import com.zx.mobile.domain.AppRegisterNewRequest;
import com.zx.platform.utils.PmpUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.Date;

public class AppRegisterCommonNewProxy {

	/**
	 * app注册合法性校验
	 * 
	 * @param appRegisterNewRequest
	 * @param messageSendLogService 
	 * @return
	 * 
	 */
	public static String checkAppRegister(
			final AppRegisterNewRequest appRegisterNewRequest, MessageSendLogService messageSendLogService) {
		if (StringUtils.isNotBlank(appRegisterNewRequest.getTelephone())) {
			if (!PmpUtils.isMobileNo(appRegisterNewRequest.getTelephone())) {
				return "手机号不合法";
			}
		} else {
			return "输入项必填";
		}
		if (StringUtils.isBlank(appRegisterNewRequest.getLoginPassword())) {
			return "输入项必填";
		}
		if (StringUtils.isNotBlank(appRegisterNewRequest.getVerifyCode())) {
			if (!messageSendLogService.checkVerifyCode(appRegisterNewRequest.getTelephone(),appRegisterNewRequest.getVerifyCode(),MobileCommonHelper.MESSAGETYPE_REGISTER)) {
				return "验证码不正确";
			}
		} else {
			return "输入项必填";
		}

		return null;
	}	
	

	/**
	 * app注册请求转换商户信息
	 * 
	 * @param appRegisterNewRequest
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static MerchantInfoModel transFormRequest(
			AppRegisterNewRequest appRegisterNewRequest)
			throws UnsupportedEncodingException {
		MerchantInfoModel merchantModel = new MerchantInfoModel();
		merchantModel.setCreateDate(new Date());
		merchantModel.setSysAccount(appRegisterNewRequest.getTelephone());
		merchantModel.setTelephone(appRegisterNewRequest.getTelephone());//由于app端注册均采取手机号
		merchantModel.setSysPassword(PmpUtils.encoderByMD5(appRegisterNewRequest
				.getLoginPassword()));
			merchantModel.setMerchantType(MobileCommonHelper.MERCHANTTYPE_PERSON);//商户类型
		merchantModel.setProductType(MobileCommonHelper.PRODUCTTYPE_APP);//产品类型  手机端用户
		merchantModel.setStatus(MobileCommonHelper.APPUSERSTATUS_NOTREQUEST);//设置用户状态为待实名认证状态
		return merchantModel;
	}

}
